﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text.RegularExpressions;
using CoolERP_Entities;
using CoolERP_BLL;
using System.Text;

namespace CoolERP_Web.User
{
    public partial class CPBZX_Edit : BasePage
    {
        protected string cpxbh
        {
            get { return this.GetUrlParam("cpxbh") == null ? string.Empty : this.GetUrlParam("cpxbh").ToString(); }
        }
        protected string hxcbh
        {
            get { return this.GetUrlParam("cpxbh") == null ? string.Empty : this.GetUrlParam("hxcbh").ToString(); }
        }
        protected Guid cpid
        {
            get { return this.GetUrlParam("cpid") == null ? Guid.Empty : new Guid(this.GetUrlParam("cpid").ToString()); }
        }
        protected Guid id
        {
            get { return this.GetUrlParam("id") == null ? Guid.Empty : new Guid(this.GetUrlParam("id").ToString()); }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (id != Guid.Empty)//编辑，第一次进来时才绑定
                    editBlind();
                else
                    hyplBack.NavigateUrl = "CPBZX_Query.aspx?cpxbh=" + cpxbh + "&hxcbh=" + hxcbh + "&cpid=" + cpid.ToString();
            }
        }

        #region ====自定义事件====
        protected void editBlind()
        {
            rblLx.Enabled = false;
            tbmc.Enabled = false;
            tbbzfs.Enabled = false;
            liTitle.Text = "修改";
            using (ZL_CPBZXBLL cBLL = new ZL_CPBZXBLL())
            {
                ZL_CPBZX entity = cBLL.GetEntity(c => c.ID == id);
                liBH.Text = entity.BH;
                trBH.Visible = true;
                rblLx.SelectedValue = entity.BZXLX.ToString();
                tbmc.Text = entity.MC;
                tbwchang.Text = entity.WCHANG.ToString();
                tbwkuan.Text = entity.WKUAN.ToString();
                tbwhou.Text = entity.WGAO.ToString();
                tbnchang.Text = entity.NCHANG.ToString();
                tbnkuan.Text = entity.NKUAN.ToString();
                tbnhou.Text = entity.NGAO.ToString();
                tbtj.Text = entity.TJ.ToString();
                tbzl.Text = entity.ZL.ToString();
                tbbz.Text = entity.BZ.ToString();
                tbbzfs.Text = entity.BZFS.ToString();
                tbsl.Text = entity.SL.ToString();
                this.hdId.Value = id.ToString();
                hyplBack.NavigateUrl = "CPBZX_Query.aspx?cpxbh=" + entity.CPXBH + "&hxcbh=" + entity.CPMCBH + "&cpid=" + entity.CPID.ToString();
            }
        }

        //自动生成产品包装箱编号
        protected string createBH()
        {
            string cpbh = string.Empty;
            using (ZL_CPBLL zBLL = new ZL_CPBLL())
            {
                ZL_CP cp = zBLL.GetEntity(t => t.ID == cpid);
                cpbh = cp.BH;
            }
            using (ZL_CPBZXBLL cBLL = new ZL_CPBZXBLL())
            {
                List<ZL_CPBZX> list = cBLL.GetList(c => c.BH.StartsWith(cpbh));
                string maxBH = "";
                string lsh = "001";
                if (list.Count > 0)
                {
                    maxBH = list.Select(t => t.BH).Max();
                    lsh = (Convert.ToInt32(maxBH.Substring(maxBH.LastIndexOf('-') + 1).TrimStart('0')) + 1).ToString();
                }
                StringBuilder sb = new StringBuilder();
                sb.Append(cpbh);
                sb.Append("/");
                sb.Append(lsh.ToString().PadLeft(3, '0'));
                return sb.ToString();
            }
        }

        #endregion

        #region ====按钮事件====
        protected void btnSave_Click(object sender, ImageClickEventArgs e)
        {
            this.lbResult.Text = "";
            string resultMessage = "";
            try
            {
                using (ZL_CPBZXBLL cBLL = new ZL_CPBZXBLL())
                {
                    int result = 0;
                    ZL_CPBZX entity = new ZL_CPBZX();
                    if (id == Guid.Empty)
                    {
                        entity.ID = Guid.NewGuid();
                        entity.BH = createBH();
                        entity.CPID = cpid;
                        entity.CPXBH = cpxbh;
                        entity.CPMCBH = hxcbh;
                        entity.MC = this.tbmc.Text.Trim();
                        entity.BZXLX = Convert.ToInt32(rblLx.SelectedValue);
                        entity.BZFS = this.tbbzfs.Text.Trim();
                    }
                    else
                        entity = cBLL.GetEntity(t => t.ID == id);
                    entity.WCHANG = Convert.ToDouble(this.tbwchang.Text.Trim());
                    entity.WGAO = Convert.ToDouble(this.tbwhou.Text.Trim());
                    entity.WKUAN = Convert.ToDouble(this.tbwkuan.Text.Trim());
                    entity.NCHANG = Convert.ToDouble(this.tbnchang.Text.Trim());
                    entity.NGAO = Convert.ToDouble(this.tbnhou.Text.Trim());
                    entity.NKUAN = Convert.ToDouble(this.tbnkuan.Text.Trim());
                    entity.TJ = Convert.ToDouble(this.tbtj.Text.Trim());
                    entity.ZL = Convert.ToDouble(this.tbzl.Text.Trim());
                    entity.SL = Convert.ToDouble(this.tbsl.Text.Trim());
                    entity.BZ = this.tbbz.Text.Trim();

                    result = cBLL.SaveEntity(entity, t => t.ID != entity.ID && t.MC == entity.MC && t.CPXBH == entity.CPXBH && t.CPMCBH == entity.CPMCBH);
                    if (result == 1)
                    {
                        btnSave.Visible = false;
                        resultMessage = "保存成功";
                    }
                    else if (result == -1)
                        resultMessage = "已存在此包装箱名称，请重新输入";
                    else
                        resultMessage = "保存失败";
                }
            }
            catch (Exception ex)
            {
                resultMessage = "保存失败" + ex.Message;
            }
            finally
            {
                this.lbResult.Text = resultMessage;
            }
        }
        #endregion
    }
}